import React, {Component} from 'react';

import './index.less';

import {base} from 'nc-lightapp-front';

import language from '../../language';

import submitForm from './request/submitModal';

const {
    NCModal,
    NCButton
} = base;

const {
    Header: ModalHeader,
    Body: ModalBody,
    Footer: ModalFooter
} = NCModal;

class EditTreeNode extends Component {
    constructor(props) {
        super(props);

        this.closeModal = this.closeModal.bind(this);
        this.showEditForm = this.showEditForm.bind(this);
        this.submitTreeNodeEdit = this.submitTreeNodeEdit.bind(this);
    }
    
    submitTreeNodeEdit() {
        const {
            form, 
            orgVal, 
            currentSelectDepartment, 
            currentEditNodeType,
            syncTree
        } = this.props;
        let formData = form.getAllFormValue('keypsngrp');

        let postData = {
            model: formData,
            pk_org: orgVal.refpk
        };

        if(form.isCheckNow('keypsngrp')) {
            submitForm(postData)
                .then((res) => {
                    let treeData = syncTree.getSyncTreeValue('keyPsnGroupTree');
                    if(!Array.isArray(treeData[0].children)){
                        treeData[0].children = [];
                    }
                    if(currentEditNodeType === 'add') {
                        treeData[0].children.push(res.data[0]);
                    }
                    else {
                        let hasIt = false;
                        treeData[0].children.map((item) => {
                            if(item.refpk === currentSelectDepartment) {
                                res.data[0].children = item.children;
                                Object.assign(item, res.data[0]);
                                hasIt = true;
                            } 
                        });

                        if(hasIt === false) {
                            treeData[0].children.push(res.data[0]);
                        }
                    }
                    // syncTree.setSyncTreeData('keyPsnGroupTree', treeData);
                    // syncTree.openNodeByPk('keyPsnGroupTree', 'root');
                    // this.props.getRef('leftRef').forceUpdate();
                    this.forceUpdate();
                    this.closeModal();
                });
        }
    }

    closeModal() {
        this.props.form.EmptyAllFormValue('keypsngrp');
        this.props.updateState({
            editTreeNodeModalVisible: false
        });
    }

    showEditForm() {
        const {currentEditTreeNodeData, form} = this.props;

        form.setAllFormValue({
            keypsngrp: currentEditTreeNodeData
        });
        form.setFormStatus('keypsngrp', 'edit');
    }

    render() {
        const {
            visible,
            form,
            currentEditTreeNodeData
        } = this.props;

        return (
            <NCModal
                show={visible}
                onHide={this.closeModal}
                onEntered={this.showEditForm}
            >
                <ModalHeader
                    closeButton={true}
                >
                    {language['hi6007-000213']/** 关键人员组 */}
                </ModalHeader>
                <ModalBody>
                    {form.createForm('keypsngrp')}
                </ModalBody>
                <ModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.submitTreeNodeEdit}
                    >
                        {language['hi6007-000004']/** 确定 */}
                    </NCButton>
                    <NCButton
                        onClick={this.closeModal}
                    >
                        {language['hi6007-000005']/** 取消 */}
                    </NCButton>
                </ModalFooter>
            </NCModal>
        );
    }
}

export default EditTreeNode;